home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2078 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Question about system call
  5. Date: 18 Jan 1996 23:49:36 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan18184936@g7240065.bridge.bst.bls.com>
  8. References: <4dekv8$9ja@spider.hik.se>
  9.     <ALUN.CHAMPION.96Jan18120927@g7240065.bridge.bst.bls.com>
  10.     <822002688snz@genesis.demon.co.uk>
  11. NNTP-Posting-Host: bstfirewall.bst.bls.com
  12. In-reply-to: Lawrence Kirby's message of Thu, 18 Jan 96 22:04:48 GMT
  13.  
  14. In article <822002688snz@genesis.demon.co.uk> Lawrence Kirby <fred@genesis.demon.co.uk> writes:
  15.  
  16. :>: An example:
  17. :>
  18. :>:   write(0, (the integer value), sizeof(int));
  19.  
  20. :>1) Try:
  21. :>
  22. :>  write(0, &int_value, sizeof(int));
  23.  
  24. : That assumes (the integer value) is an lvalue, not a general expression (or
  25. : even a constant).
  26.  
  27. True, but you can always make it one.
  28.  
  29. :>2) Try:
  30. :>
  31. :>  char buf[10];
  32. :>  sprintf(buf, "%d", int_value);
  33. :>
  34. :>  write(0, buf, strlen(buf)-1);
  35.  
  36. : strlen returns the number of characters in the string before the '\0' so
  37. : you don't want the -1. sprintf returns this value anyway so there's no
  38. : need to call strlen.
  39.  
  40. I must be tired or something ;')
  41.  
  42. :>A couple of points, this is not portable, why not use the stdio library,
  43. :>this would then become
  44. :>
  45. :>1)
  46. :>  printf("%*s", sizeof(int), (char*)&int_value);
  47.  
  48. : No, %s outputs a string i.e. stops if it hits a '\0' which it may well
  49. : do within the internal representation of an integer. Also * reads an
  50. : int argument so you need (int)sizeof(int). A close standard equivalent is:
  51.  
  52. :    fwrite(&int_val, sizeof(int), 1, stdout);
  53.  
  54. Jeez, I fell asleep. This was stupid of me, it's been a while since I've
  55. used 'C' aggressively (currently using C++), and I totally forgot about
  56. this library function.
  57.  
  58. BTW, This assumes (the integer value) is an lvalue, not a general expression
  59. (or even a constant ;')
  60.  
  61. Regards
  62.  
  63.   -A.
  64.  
  65. -- 
  66. | A.Champion                |
  67.